//
//  TextModifer.swift
//  ShouChong
//
//  Created by 陈友文 on 2024/6/11.
//

import SwiftUI
import AudioToolbox

struct MainTextModifier:ViewModifier {
    var fontSize:CGFloat = 16
    var fontWeight:Font.Weight = .medium
    var foreColor:Color = Color("color_text_black")
    func body(content: Content) -> some View {
        content
            .font(Font.system(size: fontSize, weight:fontWeight, design: .rounded))
            .foregroundColor(foreColor)
    }
}

struct MainButtonModifier:ViewModifier {
    @AppStorage("curren_main_color") var currentMainColor = "color_main_01"
//    var backColor:Color = Color(currentMainColor)
    var fontSize:CGFloat = 16
    var fontWeight:Font.Weight = .medium
    var width:CGFloat = .infinity
    
    func body(content: Content) -> some View {
        content.font(Font.system(size: fontSize, weight:fontWeight, design: .rounded))
            .foregroundColor(Color("color_bg"))
            .frame(width: width, height: 56)
            .background(Color(currentMainColor))
            .cornerRadius(28)
        
    }
}


struct ShakeButton<V>:View where V:View {
    let label:V
    let action:() ->Void
    init(label: V, action: @escaping () -> Void) {
        self.label = label
        self.action = action
    }
    
    var body: some View {
        Button(action: {
//            let generator = UINotificationFeedbackGenerator()
//            generator.prepare()
//            generator.notificationOccurred(.warning)
//            AudioServicesPlaySystemSound(1520)
            let generator = UIImpactFeedbackGenerator(style: .light)
            generator.prepare()
            generator.impactOccurred()
            action()
        }, label: {
            label
        })
    }
}



extension View {
    /// Applies the given transform if the given condition evaluates to `true`.
    /// - Parameters:
    ///   - condition: The condition to evaluate.
    ///   - transform: The transform to apply to the source `View`.
    /// - Returns: Either the original `View` or the modified `View` if the condition is `true`.
    @ViewBuilder func `if`<Content: View>(_ condition: Bool, transform: (Self) -> Content) -> some View {
        if condition {
            transform(self)
        } else {
            self
        }
    }
}
